//
// SettingsDialog.cs
// 
// Custom dialog class used for altering database settings.
//
// Author:
//   Radu Tofoleanu <kenjiru.ro@gmail.com>
//
// Copyright (C) 2007-2008 Radu Tofoleanu (http://kenjiru.ro)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Data;
using System.Data.OracleClient;
using EigaLib;

namespace EigaApp
{
	public partial class SettingsDialog : Gtk.Dialog
	{
		private DbSettings dbSettings = null;
		
		public SettingsDialog()
		{
			Build();
			
			this.dbSettings = new DbSettings ();
			this.dbSettings.Load ();
			
			dbHost.Text = this.dbSettings.HostName;
			dbName.Text = this.dbSettings.DbName;
			dbUser.Text = this.dbSettings.UserName;
			dbPassword.Text = this.dbSettings.Password;
			appUserName.Text = this.dbSettings.AppUserName;
			appPassword.Text = this.dbSettings.AppPassword;
		}

		protected virtual void OnTestButtonClicked(object sender, System.EventArgs e)
		{

			detailsExpander.Visible = true;
			detailsView.Buffer.Text = "";
			
			this.dbSettings.HostName = dbHost.Text;
			this.dbSettings.DbName = dbName.Text;
			this.dbSettings.UserName = dbUser.Text;
			this.dbSettings.Password = dbPassword.Text;
			this.dbSettings.AppUserName = appUserName.Text;
			this.dbSettings.AppPassword = appPassword.Text;
			
			EigaDb eigaDb = new EigaDb (this.dbSettings);
				
			if (eigaDb.Connect ()) {
				detailsView.Buffer.Text = "M-am conectat cu succes la baza de date!\n";
				if (eigaDb.Login ()) {
					detailsView.Buffer.Text += "M-am autentificat cu succes in aplicatie!";
				} else {
					detailsView.Buffer.Text += "Nu m-am putut autentifica in aplicatie!";
				}
			} else {
				detailsView.Buffer.Text = "Nu m-am conecta la baza de date!";
			}

		}

		protected virtual void OnSaveButtonClicked(object sender, System.EventArgs e)
		{
			this.dbSettings.HostName = dbHost.Text;
			this.dbSettings.DbName = dbName.Text;
			this.dbSettings.UserName = dbUser.Text;
			this.dbSettings.Password = dbPassword.Text;
			this.dbSettings.AppUserName = appUserName.Text;
			this.dbSettings.AppPassword = appPassword.Text;
			
			this.dbSettings.Save ();
			
			detailsView.Buffer.Text = "Am salvat setarile!";
		}

		protected virtual void OnCloseButtonClicked(object sender, System.EventArgs e)
		{
			this.Destroy ();
		}
	}
}
